package com.qianfeng.merchant.interceptor;

import com.qianfeng.common.Constants;
import com.qianfeng.entity.po.LoginMerchant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * SpringBoot的拦截器
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler.getClass().isAssignableFrom(HandlerMethod.class)){
            Subject subject = SecurityUtils.getSubject();
            LoginMerchant loginMerchant = (LoginMerchant) subject.getPrincipal();
            if (loginMerchant == null){
                response.sendRedirect("/");
            }
            request.setAttribute(Constants.CURRENT_MERCHANT , loginMerchant);
        }
        return super.preHandle(request, response, handler);
    }
}
